package com.huanxing.cloud.security.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.huanxing.cloud.common.core.constant.CommonConstants;
import com.huanxing.cloud.security.exception.HxExtendOauth2Exception;

import java.io.IOException;

/**
 * 返回格式序列化
 *
 * @author lijx
 * @version 1.0
 * @date 2022/5/16 23:05
 */
public class HxExtendOauth2ExceptionSerializer extends StdSerializer<HxExtendOauth2Exception> {

  public HxExtendOauth2ExceptionSerializer() {
    super(HxExtendOauth2Exception.class);
  }

  @Override
  public void serialize(
          HxExtendOauth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
      throws IOException {
    jsonGenerator.writeStartObject();
    jsonGenerator.writeObjectField("code", CommonConstants.FAIL);
    jsonGenerator.writeStringField("msg", e.getMessage());
    jsonGenerator.writeStringField("data", e.getErrorCode());
    jsonGenerator.writeEndObject();
  }
}
